<!--
 * @Author: liyuxiao 1273661256@qq.com
 * @Date: 2022-07-26 11:48:14
 * @LastEditors: liyuxiao 1273661256@qq.com
 * @LastEditTime: 2022-08-05 16:06:02
 * @FilePath: /cscec-pms-admin/src/components/Select/SelectTreeOrg.vue
 * @Description: 组织机构树-下拉框；queryProjectTree接口：项目部节点有项目信息
-->
<template>
  <SelectTreeCommon v-bind="$attrs" v-on="$listeners" v-model="currentValue" :api="api" :needLink="needLink"
    :replaceFields="replaceFields" />
</template>
<script>
import SelectTreeCommon from '@/components/Select/SelectTreeCommon' // 组织机构
import { queryProjectTree } from '@/api/subassembly/TreeSelectOrganization' // 组织接口

export default {
  name: 'SelectTreeOrg',
  components: {
    SelectTreeCommon
  },
  props: {
    // v-model绑定的数据
    value: {
      type: [Array, String, Object, Number]
    },
    form: {
      type: Object,
    },
    // 是否联动
    needLink: {
      type: Boolean,
      default: () => false
    },
    // 替换参数集合
    replaceFields: {
      type: Object,
      default: () => ({ children: 'childTree', title: 'orgName', key: 'orgId', value: 'orgId' })
    },
    // 是否根据当前岗位层级分割树层级
    isSeperate: {
      type: Boolean,
      default: () => true
    },
    //父节点是否禁用
    parentNodeIsDisabled: {
      type: Boolean,
      default: () => true
    }
  },
  data() {
    return {
      treeList: [],//扁平化数据结构
      // 请求api
      api: async () => {
        let res = await queryProjectTree()
        // 根据当前岗位层级分割树层级
        if (this.isSeperate) {
          // 若是项目级别，则只获取第三级节点
          if (this.$currentPost().code === 'PD') {
            res.data = res.data?.childTree[0]?.childTree || []
            // 若是分公司级别，则只获取第二级节点
          } else if (this.$currentPost().code === 'BC') {
            res.data = res.data?.childTree || []
          } else {
            res.data = [res.data]
          }
          this.treeList = res.data;
          this.flatTree(res.data)
        }
        if (res.code === 200 && res.data) {
          return { code: this.SUCCESS_CODE, data: res.data }
        }
        return { code: this.SUCCESS_CODE, data: [] }
      }
    }
  },
  watch: {
    form: {
      handler(newVal, oldVal) {
        //根据当前选中的值，获取当前选中的岗位级别
        if (this.treeList.length) {
          this.flatTree(this.treeList)
        }
      },
      deep: true
    }
  },
  methods: {
    /**
     * @description: 扁平化数据
     * @param {array} data
     * @return {void}
     */
    flatTree(tree, levels = 0) {
      for (let i = 0; i < tree.length; i++) {
        const node = tree[i]
        let level = levels
        node.level = level
        if (this.$currentPost().code === 'CO' && levels != 2) {
          // 禁用父级节点
          node.disabled = this.parentNodeIsDisabled
        }
        if (node[this.replaceFields.children]) {
          level++
          if (this.$currentPost().code === 'BC') {
            // 禁用父级节点
            node.disabled = this.parentNodeIsDisabled
          }
          this.flatTree(node[this.replaceFields.children], level)
        }
        //值绑定的情况下获取当前岗位级别
        if (this.value && this.value === tree[i].orgId) {
          this.$emit("getPostInfo", tree[i])
        }
      }
    }
  },
  computed: {
    // 转换绑定的选中数据，与v-model同步
    currentValue: {
      get: function () {
        return this.value || void 0
      },
      set: function (value) {
        this.$emit('input', value)
      }
    }
  }
}
</script>
